package controllers

import (
	"blog-server/app/common/response"
	"blog-server/app/models"
	"blog-server/global"
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
)

var store = base64Captcha.DefaultMemStore

// GetCaptcha 获取验证码
func GetCaptcha(c *gin.Context) {
	var captchaData models.Captcha
	// 生成默认数字
	driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
	// 生成base64图片
	captcha := base64Captcha.NewCaptcha(driver, store)
	// 获取
	id, b64s, err := captcha.Generate()
	if err != nil {
		global.App.Log.Error("生成验证码错误")
		response.BusinessFail(c, "系统错误")
	}
	captchaData.Id = id
	captchaData.B64s = b64s
	response.Success(c, captchaData)
}

// VerifyCaptcha 校验验证码
func VerifyCaptcha(id string, value string) bool {
	if id == "" || value == "" {
		return false
	}
	// 同时在内存清理掉这个图片
	return store.Verify(id, value, true)
}
